--
-- Author: ping.wang
-- Date: 2018-09-10 18:07:04
--
local ClassRef = app.mvc.createPanelClass(...)
local string_format = string.format
local string_split = string.split
local IconRender = require("app.widget.renders.IconRender")

function ClassRef.create(node,data)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.PeerageTipsNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)
	local coinConf = GD:getCoinConfig( Enums.PropType.roseGold )
	local iconPath = GD:getImagePath(Res.coinIconPath,coinConf.bigicon)
	self._ui["Panel/Panel_2/Panel_gold/gold"]:loadTexture(iconPath)
	--专属奖励的icon暂时先都不做

	self:_refreshView(data)
	self:setContentSize( self._ui.Panel:getContentSize() )
	ccui.Helper:doLayout( self )
end

function ClassRef:_refreshView(peerageId)
	local peerageInfo = GD:queryPeerageById(peerageId)
	if peerageInfo then
		local awardGoldNum = string_split( peerageInfo.roseGold, "|" )[3]

		self._ui["Panel/Panel_1/name"]:setString(L(peerageInfo.name))
		self._ui["Panel/Panel_2/Panel_gold/text_1"]:setString(string_format(L("peerage10"),awardGoldNum))
		self._ui["Panel/Panel_2/text_2"]:setString(string_format(L("peerage11"),L(peerageInfo.name)))
		self._ui["Panel/Panel_4/text_4"]:setString(L(peerageInfo.desc))
		local Panel_3 = self._ui["Panel/Panel_3"]
		local oldX,oldY = Panel_3:getPosition()
		local bg = self._ui["Panel"]
		local bgW,bgH = bg:getContentSize2()
		if peerageInfo.governablePeerage then
			local conferTitles = string_split( peerageInfo.governablePeerage, "|" )

			for i = 1, 7 do
				local titlePanel = Panel_3:findChild("Panel_emperor_"..i)
				if i <= #conferTitles then
					titlePanel:setVisible(true)
					local titleInfo = string_split( conferTitles[i], ":" )
					local conferInfo = GD:queryPeerageById(titleInfo[1])
					if conferInfo then
						titlePanel:findChild("name_1"):setString(L(conferInfo.name).."*"..titleInfo[2])
						titlePanel:findChild("icon_1"):loadTexture(string_format("%s%s%d.png",Res.guildPositionIconPath,"emperor_icon_tip_",tonumber(conferInfo.ID)))
					end

				else
					titlePanel:setVisible(false)
				end
			end

			--根据可授予官职来决定panel_4的位置 一行是48 4行都没有则为panel_3的高度266
			local tempNum = math.ceil(#conferTitles / 2)
			if tempNum <= 4 and tempNum >0 then
				Panel_3:setVisible(true)
				local detalH = (4 - tempNum) * 48
				local panel3W, panel3H= Panel_3:getContentSize2()
				Panel_3:setContentSize(cc.size(panel3W,panel3H - detalH))
				--Panel_3:setPositionY(oldY  - detalH)
				bg:setContentSize(cc.size(bgW,bgH - detalH))
			end
		else
			Panel_3:setVisible(false)
			bg:setContentSize(cc.size(bgW,bgH - Panel_3:getContentSize().height))
		end


		--解锁奖励icon
		local awardDatas = string_split(UD:parseClassText(peerageInfo.rewardShow),";")
		for i = 1, 3 do
			if i <= #awardDatas then
				local awardInfo = string_split(awardDatas[i],"|")
				local iconRender = IconRender.create(self._ui["Panel/Panel_2/icon_"..i])
				iconRender:setVisible(true)
				iconRender:setTipEnable(true)
				iconRender:setData(
					UD:parseLootToBagItem({type = tonumber(awardInfo[1]), id = tonumber(awardInfo[2]), num = tonumber(awardInfo[3])}))
			else
				self._ui["Panel/Panel_2/icon_"..i]:setVisible(false)
			end

		end
	end
end

return ClassRef